# Hello World

You will hear the term Hello World a lot in programming. The Hello World program is usually the first program you write in any programming language that you are learning.

The Hello World program is simply a program that outputs "Hello World" to the user.

In these exercises we will be using a virtual environment to write programs for an Arduino Uno board. This virtual environment is a real simulation of the Arduino board. This means that the program you run here, can be used on a real Arduino board.

# Serial Function

We talked briefly in the previous section about functions and what they mean in software. There is a separate section that covers functions and how to write them. To start writing useful programs right away we will learn how to use functions from the Arduino library.

What is a software library?

A software library is generally defined as a collection of code that can be used by developers in their programs. You can not write everything from scratch yourself so software libraries allow us to write programs faster by reusing working code for specific tasks.

# Serial Communication

The Serial Monitor communication on an Arduino is a way to communicate between the Arduino board and a computer, usually by using a USB wire that connects to a computer. Communication means sending data. Our goal here is to send "Hello World" as a message from our program that is running on the Arduino board to our computer screen.

In our case, we don't yet have to connect anything as we are using a virtual environment. To perform the "Hello World" task there are two functions in the Arduino library we need to use.

Serial.begin(9600);

and

Serial.println("your message");

Both functions are part of the Serial library. This is why we are starting our statement with Serial and then . to access functions from this library.

The first function sets the rate of data transmission between the Arduino and the Serial Monitor. You don't have to understand fully how this works at this point. However, 9600 specifies the rate of data transmission in bits per second to be used with the Serial Monitor. Before we can transmit data we must begin the serial.

After beginning the Serial we can then use the second function to print a full line onto the Serial output.

How to represent a line in C++?

To write a line to the function we will need to start with " and finish the line with ". This is known as a string of characters.


The function is called println which means "print line". After it prints the characters we pass to it, the function will print a new line character at the end.

What is a new line character?

There is a special character a computer recognizes that represents starting a new line. You usually do not see this character on a website or in a word processing document because it gets automatically rendered as a new line in your text by the software.

On the Arduino platform the new line character is \n. So if you use the print function as follows

Serial.println("your\n message");

notice that adding \n between "your" and "message" gives an output in the Serial as

your
message

instead of what we might have expected:

your message

We will use both of these Arduino library functions inside the Setup since we want the Arduino to send Hello World only once when the Arduino is first turned on.

Exercise

Instead of printing Hello World, print

Hello 
Computer
Answer
void setup() 
{
  Serial.begin(9600);
  
  Serial.println("Hello\n Computer");
}
void loop() 
{

}

# Semicolon

In C++ a semicolon is needed at the end of every full statement. In our Hello World example notice at the end of every line inside the Setup function there is a semicolon to terminate a statement.

This is part of the syntax of a program in C++. If we write the program with the wrong syntax, the compiler will give us errors and the program will not run.

For example, if we run the following program:

void setup() 
{
 Serial.begin(9600);
 
 Serial.println("Hello Computer") //removed the semicolon from here
}
void loop() 
{

}

The output of the compiler will be

/sketch/sketch.ino: In function 'void setup()':
/sketch/sketch.ino:6:1: error: 
expected ';' before '}' token } 
^
Error during build: exit status 1

You can try it yourself. Remove the semicolon from the program and run it in the above Arduino environment.

The error in many cases explains the problem well. In this case the compiler is first telling us that there is an error inside the setup function

/sketch/sketch.ino: In function 'void setup()':

It is also telling us at what line the error is happening (line 6) along with the first character of that line. You can see it in the second line .ino:6:1

/sketch/sketch.ino:6:1: error: 

And then the compiler is explaining to us what it thinks the problem is. On line 6 the compiler found the character } and it was expecting ;. The semicolon was expected at the end of line 5.

Exercise

The following program has 2 errors. Fix the errors to get the program running and output Hello Dawson.



# Delay Function

As we have stated, the Arduino will first run the code inside the Setup function once and then keep on running the code inside the loop function repeatedly until we turn off the Arduino.

However, sometimes, we will need to tell the Arduino to sleep for a while. For example ,let's say we want to print "Hello World" once every second, forever.

This task sounds suitable for the loop function.

void setup() 
{
 Serial.begin(9600);
 
}
void loop() 
{
 Serial.println("Hello World");
}

If we write our program like this then the Arduino will output our message repeatedly too fast. What we want the Arduino to do is to sleep for 1 second before the beginning of every loop.

There is a function in the Arduino library called delay(TIME_IN_MILLESECONDS). We can pass the time we want the Arduino to sleep for to this function so that the Arduino will not continue processing our lines of code until the time we specify has past. The time we pass is in milliseconds.

The following program will wait for 1 second (1000 millisecond) at the end of every loop iteration.

A few things to note:

  • We used begin in the Setup function for the Serial because we only need to start the Serial once at the beginning of the program. If we put it inside the loop then at every loop iteration the Serial will begin again.

  • At the end of delay(1000) we have a semicolon to end this statement. If we don't put a semicolon we will get a compiler error as before.